package com.jhgk.jhcee.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jhgk.jhcee.common.domain.TableListModel;
import com.jhgk.jhcee.common.enums.BasisResultEnum;
import com.jhgk.jhcee.common.resultful.BasisResultful;
import com.jhgk.jhcee.data.auth.entity.SysMenuInfo;
import com.jhgk.jhcee.data.auth.entity.SysRoleInfo;
import com.jhgk.jhcee.data.auth.mapper.SysMenuInfoMapper;
import com.jhgk.jhcee.data.auth.mapper.SysRoleInfoMapper;
import com.jhgk.jhcee.system.domain.dao.SysMenuInfoDao;
import com.jhgk.jhcee.system.domain.dao.SysRoleInfoDao;
import com.jhgk.jhcee.system.domain.model.SysRoleInfoModel;
import com.jhgk.jhcee.system.service.SysMenuInfoService;
import com.jhgk.jhcee.system.service.SysRoleInfoService;
import com.jhgk.jhcee.system.utils.TransitionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiezhongping
 * @description:
 * @date: 2021/8/25 10:47.
 */
@Service(value = "SysRoleInfoServiceImpl")
@Slf4j
public class SysRoleInfoServiceImpl extends ServiceImpl<SysRoleInfoMapper, SysRoleInfo> implements SysRoleInfoService {

    @Autowired
    SysMenuInfoDao sysMenuInfoDao;

    @Autowired
    SysRoleInfoDao sysRoleInfoDao;

    @Autowired
    SysMenuInfoMapper sysMenuInfoMapper;

    @Autowired
    SysRoleInfoMapper sysRoleInfoMapper;

    @Resource(name = "SysMenuInfoServiceImpl")
    SysMenuInfoService sysMenuInfoService;

    @Override
    public List<SysRoleInfo> getRoleList(Long userId) {
        return sysRoleInfoMapper.getRoleList(userId);
    }

    @Override
    public List<SysMenuInfo> getMenuList(Long userId) {
        return sysMenuInfoMapper.getMenuList(userId);
    }

    @Override
    public BasisResultful<Object> roleAllList(SysRoleInfoModel model) {
        if (null == model) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        PageHelper.startPage(model.getPage(), model.getPageSize());
        QueryWrapper<SysRoleInfo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(model.getOrder()) && "ascend".equals(model.getOrder()) && StringUtils.isNotBlank(model.getField())) {
            wrapper.orderByAsc(TransitionUtil.toUnderLowercase(model.getField()));
        }
        if (StringUtils.isNotBlank(model.getOrder()) && StringUtils.isNotBlank(model.getField()) && "descend".equals(model.getOrder())) {
            wrapper.orderByDesc(TransitionUtil.toUnderLowercase(model.getField()));
        }
        if (StringUtils.isBlank(model.getField())) {
            wrapper.orderByAsc("sort");
        }
        List<SysRoleInfo> sysRoleInfos = sysRoleInfoMapper.selectList(wrapper);
        if (sysRoleInfos.isEmpty()) {
            return BasisResultful.success(model);
        }
        List<SysRoleInfoModel> models = new ArrayList<>();
        sysRoleInfos.forEach(role -> {
            SysRoleInfoModel roleInfoModel = new SysRoleInfoModel();
            BeanUtil.copyProperties(role,roleInfoModel);
            roleInfoModel.setId(String.valueOf(role.getRoleId()));
            // 查询所拥有的菜单
            roleInfoModel.setMenu(sysMenuInfoService.getRoleMenu(role.getRoleId()));
            models.add(roleInfoModel);
        });
        PageInfo<SysRoleInfoModel> pageInfo = new PageInfo<>(models);
        return BasisResultful.success(new TableListModel(pageInfo.getList(), pageInfo.getTotal()));
    }

    @Override
    public BasisResultful<Object> addRole(SysRoleInfoModel sysRoleInfoModel) {
        if (null == sysRoleInfoModel) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        boolean save = this.save(sysRoleInfoModel);
        return save ? BasisResultful.success() : BasisResultful.fail();
    }

    @Override
    public BasisResultful<Object> updateRole(SysRoleInfoModel sysRoleInfoModel) {
        if (null == sysRoleInfoModel) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        QueryWrapper<SysRoleInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", sysRoleInfoModel.getRoleId());
        boolean update = this.update(sysRoleInfoModel, wrapper);
        if (!update) {
            return BasisResultful.fail();
        }
        // 更新所属菜单
        boolean aBoolean = sysMenuInfoService.saveRoleMenu(sysRoleInfoModel.getRoleId(), sysRoleInfoModel.getMenu());
        if (!aBoolean) {
            return BasisResultful.fail();
        }
        return BasisResultful.success();
    }

    @Override
    public BasisResultful<Object> deleteRole(SysRoleInfoModel sysRoleInfoModel) {
        if (null == sysRoleInfoModel) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        QueryWrapper<SysRoleInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", sysRoleInfoModel.getRoleId());
        int delete = sysRoleInfoMapper.delete(wrapper);
        return delete > 0 ? BasisResultful.success() : BasisResultful.fail();
    }

    @Override
    public BasisResultful<Object> setRoleStatus(Long roleId, Integer status) {
        if (null == roleId || null == status) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        sysRoleInfoMapper.updateStatus(roleId, status);
        return BasisResultful.success();
    }

    @Override
    public BasisResultful<Object> getRoleAll() {
        List<SysRoleInfo> sysRoleInfos = sysRoleInfoMapper.selectList(null);
        return BasisResultful.success(sysRoleInfos);
    }

    /**
     * 大写转下划线小写
     */
}
